import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../res/my_colors.dart';

class ActionSheet extends StatefulWidget {
  String title;
  List<dynamic> items;
  String displayKey;
  ActionSheet({Key? key, this.title = "", required this.items, this.displayKey = "name"}) : super(key: key);

  @override
  _ActionSheetState createState() => _ActionSheetState();
}

class _ActionSheetState extends State<ActionSheet> {
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.all(8),
        child: Column(
          children: [
            // 占位 把真的控件挤到底下去
            Expanded(
              flex: 1,
              child: GestureDetector(
                onTap: () {Get.back();},
                child: Container(color: Colors.transparent,),
              ),
            ),
            // 标题行
            Container(
              width: double.infinity,
              decoration:
                  BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(topLeft: const Radius.circular(16), topRight: Radius.circular(16))),
              padding: EdgeInsets.only(top: 8, bottom: 8),
              child: Text(
                widget.title,
                style: TextStyle(color: Colors.grey.shade400, fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ),
            Container(
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: BorderRadius.only(bottomLeft: const Radius.circular(16), bottomRight: const Radius.circular(16))),
                width: double.infinity,
                // height: 300,
                constraints: BoxConstraints(maxHeight: 300),
                child: /*Column(
                children: [
                  Container(
                    padding: EdgeInsets.only(top: 8, bottom: 8),
                    child: Text("data", style: TextStyle(color: Colors.grey.shade400, fontWeight: FontWeight.bold),),
                  ),
                  Expanded(child: */
                    ListView.separated(
                  itemBuilder: (context, index) {
                    return SizedBox(
                      width: double.infinity,
                      height: 48,
                      child: MaterialButton(
                        onPressed: () {
                          Get.back(result: _getResult(index));
                        },
                        child: Text(
                          _getDisplayName(widget.items[index]),
                          style: TextStyle(color: Colors.black87, fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider(height: 1, color: MyColors.dividerColor,);
                  },
                  itemCount: widget.items.length,
                  shrinkWrap: true,
                ) /*, flex: 1,)
                ],
              ),*/
                ),
            Container(
              margin: EdgeInsets.only(top: 8),
              width: double.infinity,
              height: 48,
              child: MaterialButton(
                color: Colors.white,
                onPressed: () {
                  Get.back();
                },
                child: Text(
                  "取消",
                  style: TextStyle(color: Colors.black87, fontSize: 16, fontWeight: FontWeight.bold),
                ),
              ),
            )
          ],
        ));
  }

  String _getDisplayName(dynamic item) {
    if (item is String) {
      return item;
    }  else if (item is Map) {
      return item[widget.displayKey];
    }
    return "";
  }

  dynamic _getResult(int index) {
    var item = widget.items[index];
    if (item is String) {
      return index;
    }  else {
      return item;
    }
  }
}
